package com.cloud.adrian.novel.user.config;

import com.cloud.adrian.novel.common.constant.ApiRouterConstants;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * Spring Web Mvc 相关配置不要加 @EnableWebMvc 注解，否则会导致 jackson 的全局配置失效。
 * 因为 @EnableWebMvc 注解会导致 WebMvcAutoConfiguration 自动配置失效
 *
 * @Author: xiangguiliang
 * @version: 1.0
 * @since: 2024/05/25
 */
@Configuration
@RequiredArgsConstructor
public class WebConfig implements WebMvcConfigurer {
    private final AuthInterceptor authInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(authInterceptor)
                // 拦截用户相关操作（发评论等）
                .addPathPatterns(ApiRouterConstants.API_FRONT_USER_URL_PREFIX + "/**")
                // 排除登录和注册接口
                .excludePathPatterns(ApiRouterConstants.API_FRONT_USER_URL_PREFIX + "/login",
                        ApiRouterConstants.API_FRONT_USER_URL_PREFIX + "/register").order(2);
    }
}
